#include "common.h"
#include <signal.h>

int global = 10;

void signal_handler(int signum)
{
	printf("rev SIGALRM num : %d\n", signum);
	global = 5;
}

void signal_handler_sigint(int signum)
{
	printf("rev SIGINT num : %d\n", signum);
	int i = 0;
	while(i < 10)
	{
		global = 0;
		sleep(1);
		printf("global2 num : %d\n", global);
		i++;
	}

	if(i >= 10)
	{
		struct sigaction act;
		act.sa_handler = signal_handler_sigint;
		sigemptyset(&act.sa_mask);
		sigdelset(&act.sa_mask, SIGALRM);
		act.sa_flags = 0;

		sigaction(SIGINT, &act, NULL);
		signal(SIGALRM, signal_handler);
	}
}

int main()
{
	pid_t pid;
	pid = fork();

	if(pid < 0)
		err_sys("fork error\n");
	else if(pid == 0)
	{
		while(1)
		{
			sleep(3);
			kill(getppid(), SIGALRM);
		}
	}
	else
	{
		signal(SIGALRM, signal_handler);

		struct sigaction act;
		act.sa_handler = signal_handler_sigint;
		sigemptyset(&act.sa_mask);
		sigaddset(&act.sa_mask, SIGALRM);
		act.sa_flags = 0;

		sigaction(SIGINT, &act, NULL);

		while(1)
		{
			sleep(1);
			printf("global num : %d\n", global);
		}

		pause();
	}

	return 0;
}
